<template>
    <div>
       <h3>商品列表页面</h3>
       <Breadcrumb :breadcrumba="breadcrumba" :breadcrumbb="breadcrumbb"></Breadcrumb>
       <el-card>
           <!-- 搜索框 -->
            <el-row :gutter="20">
                <el-col :span="6">
                    <div>
                        <el-input placeholder="请输入内容" v-model="goodsInfo.query"  clearable class="input-with-select" @clear="getGoods">
                            <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
                        </el-input>
                    </div>
                </el-col>
            <el-col :span="12">
                <el-button type="primary" @click="goAddpage">添加商品</el-button>
            </el-col>
            </el-row>
            <!-- 表格 -->
           <el-table
            :data="goodsList"
            border
            style="width: 100%"
             height="500"
             stripe>
             <el-table-column
            type="index"
            label="序号"
            width="50">
            </el-table-column>
            <el-table-column
            prop="goods_name"
            label="商品名字"
            width="250"
            align="center">
            </el-table-column>
            <el-table-column
            prop="goods_price"
            label="价格"
            width="100"
            align="center">
            </el-table-column>
            <el-table-column
            prop="goods_number"
            label="库存"
            width="100"
            align="center">
            </el-table-column>
            <el-table-column
            prop="goods_weight"
            label="重量"
            width="100"
            align="center">
            </el-table-column>
            <el-table-column
            prop="goods_state"
            label="状态"
            width="100"
            align="center">
            </el-table-column>
            <el-table-column
            prop="add_time"
            label="添加时间"
            align="center">
            <!-- 作用域插槽 -->
                <template slot-scope="scope">
                    {{scope.row.add_time | formatDate}}
                </template>
            </el-table-column>
            <el-table-column
            prop="hot_mumber"
            label="热门词"
            align="center">
            </el-table-column>
            <el-table-column
            prop="is_promote"
            label="是否促销"
            align="center">
            </el-table-column>
            <el-table-column
            label="操作"
            align="center"
            fixed="right">
                <template slot-scope="scope">
                   <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row)">修改</el-button>
                   <el-button type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="goodsInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="goodsInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
       </el-card>

       <!-- 添加商品 -->
       <el-dialog
        title="添加商品"
        :visible.sync="addGoodsDialogVisible"
        width="30%"
        >
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addGoodsDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addGoodsDialogVisible = false">确 定</el-button>
        </span>
        </el-dialog>

    </div>
</template>

<script>
import Breadcrumb from "../../components/Breadcrumb"
import {getGoods,delGoods,addGoods} from "../../api/goodss"
    export default {
        components:{
            Breadcrumb
        },
        data(){
            return{
                breadcrumba:'',
                breadcrumbb:'',
                goodsInfo:{
                    query:"",
                    pagenum:1,
                    pagesize:5
                },
                total:0,
                goodsList:[],
                addGoodsDialogVisible:false,
                addGoodsInfo:{}
                // search:''
            }
        },
        created(){
            this.breadcrumba=sessionStorage.getItem('breadcrumba')
            this.breadcrumbb=sessionStorage.getItem('breadcrumbb')
            this.getGoods()
        },
        methods:{
            async getGoods(){
                let {data:res} = await getGoods(this.goodsInfo)
                if(res.meta.status !== 200){
                    return this.$message.error('获取商品列表失败！')
                }
                this.goodsList = res.data.goods
                this.total = res.data.total
                console.log(res,"获取商品返回值");
            },
            // 修改
            edit(row){
                console.log(row,"修改的row");
            },
            del(row){
                console.log(row,"删除的row");
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(async () => {
                    let {data:res} = await delGoods(row.goods_id)
                    // console.log(res,'删除之后返回的值！');
                    if(res.meta.status !== 200){
                       return this.$message.error('删除商品失败！')
                    }
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getGoods()
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                })
            },
            handleSizeChange(newPagesize) {
                // console.log(`每页 ${val} 条`);
                this.goodsInfo.pagesize = newPagesize
                this.getGoods()
            },
            handleCurrentChange(newPagenum) {
                // console.log(`当前页: ${val}`);
                this.goodsInfo.pagenum = newPagenum
                this.getGoods()
            },
            searchGoods(){
                console.log(this.goodsInfo.query);
                this.getGoods()
            },
            // 添加 商品
            goAddpage(){
                this.$router.push('/goods/add')
            }
        }
    }
</script>

<style scoped>
    .el-card{
        margin-top: 15px;
    }
    .el-table{
        margin: 10px 0;
    }
</style>